import { getMemberAddressAPI } from '@/services/address'
import type { AddressItem } from '@/types/address'
import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useAddressStore = defineStore('address', () => {
  const selectedAddress = ref<AddressItem | null>()
  const changeSelectedAddress = (val: AddressItem) => {
    selectedAddress.value = val
  }
  const addressList = ref<AddressItem[]>([])
  const getMemberAddressData = async () => {
    const res = await getMemberAddressAPI()
    addressList.value = res.result
  }
  const selectedDefaultAddress = () => {
    selectedAddress.value = addressList.value.find((address) => address.isDefault === 1)
  }
  return {
    selectedAddress,
    changeSelectedAddress,
    getMemberAddressData,
    addressList,
    selectedDefaultAddress,
  }
})
